﻿using Business.Base;
using Business.Common;
using Configuration;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


namespace Business.CentrifugalPump.Perf
{
    public class CentrifugalPumpPerfExperimentRecordDBManager:IDBManager
    {
      
        private String fields = @"experimentCode, recordTime, workFlow, presureIn, presureOut, electricCurrent, voltage, workingPower,
                workingSpeed, temperature, workingHead, stpFlow, stpHead, stpShaftPower, stpPumpEfficiency, stpGroupEfficiency,
                workingStaticHead, workingDynamicHead, workingGeoHead, workingFrictionHead";
        private String insertFields = @"@experimentCode, @recordTime, @workFlow, @presureIn, @presureOut, @electricCurrent, @voltage, @workingPower,
                @workingSpeed, @temperature, @workingHead, @stpFlow, @stpHead, @stpShaftPower, @stpPumpEfficiency, @stpGroupEfficiency,
                @workingStaticHead, @workingDynamicHead, @workingGeoHead, @workingFrictionHead";
        private String table = "perfExperimentRecord";
        SqlConnection connection = null;

        private SqlCommand sqlSelectCountCommand = null;
        private SqlCommand sqlSelectCommand = null;
        private SqlDataAdapter sqlSelectAdapter = null;
        private SqlCommand sqlInsertCommand = null;
        private SqlCommand sqlDeleteCommand = null;

        
        

        public CentrifugalPumpPerfExperimentRecordDBManager()
        {
            String connectionString = DataSourceConfig.DataSourceString;
            connection = new SqlConnection(connectionString);

            sqlSelectCountCommand = new SqlCommand();
            sqlSelectCountCommand.Connection = connection;

            sqlSelectCommand = new SqlCommand();
            sqlSelectCommand.Connection = connection;

            sqlInsertCommand = new SqlCommand();
            sqlInsertCommand.Connection = this.connection;
            sqlInsertCommand.CommandText = "INSERT INTO [" + table + "] (" + fields + ") VALUES ("+insertFields+")";

            sqlDeleteCommand = new SqlCommand();
            sqlDeleteCommand.Connection = this.connection;
            //sqlDeleteCommand.CommandText = "delete from " + table;

            sqlSelectAdapter = new SqlDataAdapter();
            sqlSelectAdapter.SelectCommand = sqlSelectCommand;
        }



        public int GetCount(BaseQuery query)
        {
            throw new NotImplementedException();
        }

        public DataTable GetDataList(BaseQuery query)
        {
            throw new NotImplementedException();
        }

        public int Insert(object obj)
        {
            CentrifugalPumpPerfExperimentRecord perfExperimentRecord = (CentrifugalPumpPerfExperimentRecord)obj;

            sqlInsertCommand.Parameters.Add(new SqlParameter("@experimentCode", perfExperimentRecord.ExperimentCode));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@recordTime", perfExperimentRecord.RecordTime));
            //sqlInsertCommand.Parameters.Add(new SqlParameter("@experiment", perfExperimentRecord.Experiment));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workFlow", perfExperimentRecord.WorkFlow));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@presureIn", perfExperimentRecord.PresureIn));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@presureOut", perfExperimentRecord.PresureOut));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@electricCurrent", perfExperimentRecord.ElectricCurrent));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@voltage", perfExperimentRecord.Voltage));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingPower", perfExperimentRecord.WorkingPower));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingSpeed", perfExperimentRecord.WorkingSpeed));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@temperature", perfExperimentRecord.Temperature));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingHead", perfExperimentRecord.WorkingHead));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@stpFlow", perfExperimentRecord.StpFlow));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@stpHead", perfExperimentRecord.StpHead));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@stpShaftPower", perfExperimentRecord.StpShaftPower));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@stpPumpEfficiency", perfExperimentRecord.StpPumpEfficiency));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@stpGroupEfficiency", perfExperimentRecord.StpGroupEfficiency));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingStaticHead", perfExperimentRecord.WorkingStaticHead));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingDynamicHead", perfExperimentRecord.WorkingDynamicHead));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingGeoHead", perfExperimentRecord.WorkingGeoHead));
            sqlInsertCommand.Parameters.Add(new SqlParameter("@workingFrictionHead", perfExperimentRecord.WorkingFrictionHead));

            connection.Open();
            int id = (int)sqlInsertCommand.ExecuteScalar();
            sqlInsertCommand.Parameters.Clear();
            connection.Close();
            return id;

        }

        public void Delete(Object id)
        {
            String experimentCode = (String)id;
             sqlDeleteCommand.CommandText = "delete from " + table + " where experimentCode = @experimentCode ";
             sqlDeleteCommand.Parameters.Add(new SqlParameter("@experimentCode", (string)experimentCode));
            
            connection.Open();
            sqlDeleteCommand.ExecuteNonQuery();
            sqlDeleteCommand.Parameters.Clear();
            connection.Close();
        }

        public void DeleteByCondtion(String condition)
        {
            if (condition != null)
            {
                sqlDeleteCommand.CommandText = "delete from " + table + " where " + condition;
            }
            else
            {
                return ;
            }
            connection.Open();
            sqlDeleteCommand.ExecuteNonQuery();
            sqlDeleteCommand.Parameters.Clear();
            connection.Close();
        }

    }
}
